﻿//*** Guidance generated code ***//
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Web.Compilation;

using ProjectBase.Core;

namespace $safeprojectname$.Providers
{
    public class ResourceFactory : ResourceProviderFactory
    {
        /// <summary>
        ///     Creates ResourceProvider based on area. use on ASP.NET pages and controls:
        ///     Text="&lt;%$ Resources:CommonTerms, Hello %&gt;"  
        /// </summary>
        /// <param name = "area">(classKey) the area - more detailed selector for localization</param>
        /// <returns></returns>
        public override IResourceProvider CreateGlobalResourceProvider(string area)
        {
            // Text="<%$ Resources:CommonTerms, Hello %>"  
            var result = new ResourceProvider(area);
            return result;
        }

        /// <summary>
        ///     Creates LocalResource provider based on entity (controller) name
        /// </summary>
        /// <param name = "virtualPath"></param>
        /// <returns></returns>
        public override IResourceProvider CreateLocalResourceProvider(string virtualPath)
        {
            var area = virtualPath; // let's convert the page or webcontrol path into controller(entity) name
            if (virtualPath.Contains("/Views/"))
            {
                area = virtualPath.Remove(0, virtualPath.IndexOf("Views/") + 6);
                // now we have controller name and appendix
                area = area.Contains("/")
                           ? area.Split('/')[0]
                           : area;
            }
            else if (virtualPath.Contains("/Controls/"))
            {
                area = virtualPath.Remove(0, virtualPath.IndexOf("Controls/") + 9);
                // now we have controller name and appendix
                if (area.Split('/').Length > 2)
                {
                    area = area.Split('/')[1]; // expect that there is a namespace in the path
                }
                else if (area.Split('/').Length == 2)
                {
                    area = area.Split('/')[0]; // no namespace - after controls is the name of controller(entity)
                }
            }
            var result = new ResourceProvider(area);
            return result;
        }
    }
}